import { ValidationRule } from 'quasar';

// prettier-ignore
const REGEX_EMAIL = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// prettier-ignore
const REGEX_URL =  /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
const REGEXP_TEL = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;

export const required: ValidationRule = (v) => {
  return (v !== null && v !== undefined && String(v).length > 0) || '必填项';
};

export const date: ValidationRule = (v) => isEmpty(v) || /^\d{4}[\/|-](0[1-9]|1[0-2])[\/|-]([0-2]\d|30|31)$/.test(v) || '格式 yyyy-MM-dd';
export const time: ValidationRule = (v) => isEmpty(v) || /^([0-1]?\d|2[0-3]):[0-5]\d$/.test(v) || '格式 mm:ss';
export const email: ValidationRule = (v) => isEmpty(v) || REGEX_EMAIL.test(v) || '格式错误';
export const numeric: ValidationRule = (v) => isEmpty(v) || /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(v) || '必须是数字';
export const integer: ValidationRule = (v) => isEmpty(v) || /^[+-]?(0|([1-9]\d*))$/g.test(v) || '必须是整数';
export const gt = (m: number, msg = '大于' + m) => ((v) => isEmpty(v) || Number(v) > m || msg) as ValidationRule;
export const goe = (m: number, msg = '大于等于' + m) => ((v) => isEmpty(v) || Number(v) >= m || msg) as ValidationRule;
export const lt = (m: number, msg = '小于' + m) => ((v) => isEmpty(v) || Number(v) < m || msg) as ValidationRule;
export const loe = (m: number, msg = '小于等于' + m) => ((v) => isEmpty(v) || Number(v) <= m || msg) as ValidationRule;
// prettier-ignore
export const between = (min: number, max: number, msg = `范围[${min} , ${max}]`) =>  ((v) => isEmpty(v) || (Number(v) <= max && Number(v) >= min) || msg) as ValidationRule;

export const length = (len: number, msg = '限定长度 ' + len) => ((v) => isEmpty(v) || String(v).length === len || msg) as ValidationRule;
export const maxLength = (len: number, msg = '长度 <= ' + len) => ((v) => isEmpty(v) || String(v).length <= len || msg) as ValidationRule;
export const minLength = (len: number, msg = '长度 >= ' + len) => ((v) => isEmpty(v) || String(v).length >= len || msg) as ValidationRule;

export const idCard: ValidationRule = (v) => {
  if (isEmpty(v)) return true;
  const idEighteen = /^([1-6][1-9]|50)\d{4}(18|19|20|21)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  const idFifteen = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/;
  return idEighteen.test(String(v)) || idFifteen.test(String(v)) || '身份证号格式不正确';
};

// 正则表达式
export const regex = (regex: string | RegExp, msg = '格式:' + regex): ValidationRule => {
  if (typeof regex === 'string') {
    regex = new RegExp(regex);
  }
  return (v) => isEmpty(v) || (regex as RegExp).test(String(v)) || msg;
};

export const chinese: ValidationRule = regex(/^[\u4e00-\u9fa5]{0,}$/, '只能填写中文');
export const url = regex(REGEX_URL, 'URL格式不正确');
export const telephone = regex(REGEXP_TEL, '手机号格式不正确');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isEmpty(value: any): boolean {
  if (value === null || value === undefined || value === '') {
    return true;
  }
  return false;
}
